﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TCP.SocketHelper;

namespace TCP.Test.Client
{
    class Program
    {
        public static SocketClient client = null;
        public static void Main(string[] args)
        {
            string ip = "127.0.0.1";
            int port = 8080;
            if (args != null && args.Length > 0)
            {
                int len = args.Length;
                for (int i = 0; i < len; i++)
                {
                    switch (args[i])
                    {
                        default:
                            Console.WriteLine("invaild param:"+args[i]);
                            break;
                        case "-s":
                            i++;
                            ip = args[i];
                            break;
                        case "-p":
                            i++;
                            port = int.Parse(args[i]);
                            break;
                    }
                }
            }

            client = new SocketClient(ip, port);

            client.LogAction += Log;
            client.ReceiveMsgAction += ReceiveData;
            client.OnConnectSuccrssAction += ConnectSuccess;
            client.StartConnect();
            string cmd = "";
            do
            {
                cmd = Console.ReadLine();
                switch (cmd.ToLower())
                {
                    case "clear":
                        Console.Clear();
                        break;
                    case "send":
                        Console.Write("Content:");
                        string conetnet = Console.ReadLine();
                        client.SendMessage(Encoding.UTF8.GetBytes(conetnet));
                        break;
                }
                
            } while (cmd.ToLower() != "exit");
        }

        public static void ConnectSuccess()
        {
            client.SendName("Test.Client");
        }

        public static void Log(object log,int type)
        {
            Console.WriteLine(log);
        }

        public static void ReceiveData(byte[] data)
        {
            string str = Encoding.UTF8.GetString(data);
            Console.WriteLine("str:"+str);
        }
    }
}
